<?php

namespace app\controller;

use app\BaseController;
use think\facade\View;
use app\model\User;
use think\facade\Db;
use app\model\Site;
use app\utils\Utils;
use think\facade\Session;
use app\model\Log;


class Index extends BaseController
{
    public function index($p = 1) // 首页
    {
        $posts = Db::connect()->table('site_post')->order(['ison' => 'desc', 'isbest' => 'desc', 'ishot' => 'desc', 'goods' => 'desc', 'time' => 'desc', 'views' => 'desc'])->page(intval($p), 3)->select()->toArray(); // 查询帖子数据
        $result = array();
        foreach ($posts as $key) {
            $user_id = $key['user_id'];
            $userinfo = User::where('id', $user_id)->select()->toArray()[0]; // 查询数据
            $key["user_img"] = $userinfo['img'];
            $key["user_id"] = $user_id;
            $key["user_name"] = $userinfo['name'];
            $key["text"] = mb_strimwidth($key["text"], 0, 150, '...', "utf-8");
            $key["time"] = date('Y-m-d', $key["time"]);
            $result[] = $key;
        }

        $email = Session::get('email', '');
        $pw = Session::get('password', '');
        $my_user_info = Utils::user($email, $pw);

        return View::fetch(Site::get('template') . '/home', [
            'img' => Site::get('img'),
            'title' => Site::get('title'),
            'navitems' => Site::get('navitems'),
            'user_name' => $my_user_info["name"],
            'user_email' => $my_user_info["email"],
            'user_img' => $my_user_info["img"],
            'user_id' => $my_user_info["id"],
            'about' => Site::get('about'),
            'header' => Site::get("header"),
            'footer' => Site::get("footer"),
            'footer_about' => Site::get("footer_about"),
            'footer_list' => Site::get('footer_list'),
            'post' => $result,
            'logo' => Site::get('logo')
        ]);
    }

    public function clientarea() // clientarea
    {
        return View::fetch(Site::get("template") . '/clientarea', [
            'img' => Site::get('img'),
            'title' => Site::get('title'),
            'user_name' => 'Demo',
            'user_id' => '0',
            'user_ip' => Utils::getIp()
        ]);
    }


    public function sitemap()
    {
        $posts = Db::connect()->table('site_post')->order(['ison' => 'desc', 'isbest' => 'desc', 'ishot' => 'desc', 'goods' => 'desc', 'time' => 'desc', 'views' => 'desc'])->where('time', '>', time() - 3600 * 24 * 30 * 3)->select()->toArray(); // 查询帖子数据
        $xml_text = "<?xml version='1.0' encoding='UTF-8'?>
        <urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'   >";

        foreach ($posts as $key) {
            $xml_text .= "        <url>
            <loc>" . Site::get('url') . 'post/' . $key['id'] . "</loc>
            <priority>0.9</priority>
            <lastmod>" . date('Y-m-d', $key['time']) . "</lastmod>
            <changefreq>always</changefreq>
        </url>";
        }
        return xml($xml_text . '    </urlset>');
    }
}
